define(['moment'
,'module/moduleSMT/program/SMT26/closeItem.service'
], function () {
    return ['$scope', '$rootScope', '$filter', '$stateParams', '$mdDialog', 'config', 'MMWService', '$calculater','$ClosItem',
        function ($scope, $rootScope, $filter, $stateParams, $mdDialog, config, $MMWService, $calculater,$ClosItem) {

            $scope.init = function () {
                $scope.pick_list = [];
                $scope.detail_list = [];
                $scope.real_need_qty = 0;
                let default_modeList = [
                    {name:$filter('translate')('common.report.condition.equal'), code:'0'},
                    {name:$filter('translate')('common.report.condition.contain'), code:'2'}
                ];
                $scope.C00_require_condition = false;
                $scope.codeList = [
                    {name:$filter('translate')('SMT26.query_condition_list.column1'), column_name:'PickListID',modeList:default_modeList},//需求單號
                    {name:$filter('translate')('SMT26.query_condition_list.column2'), column_name:'PickListType',modeList:default_modeList},//需求單類型
                    {name:$filter('translate')('SMT26.query_condition_list.column3'), column_name:'OPNo',modeList:default_modeList},//作業站編號
                    {name:$filter('translate')('SMT26.query_condition_list.column4'), column_name:'EquipmentNo',modeList:default_modeList},//設備編號
                    {name:$filter('translate')('SMT26.query_condition_list.column5'), column_name:'MoNo',modeList:default_modeList},//工單編號
                ];
            };

            $scope.back = function () {
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('home', true);
            };

            let key;
            let reel_used_list=[];//做一個陣列放刷過的捲料料號，方便提示已刷過
            $scope.input_barcode = function(barcode){
                if(barcode!==''){
                    var reused = reel_used_list.some(function(item){
                        return item == barcode;
                    });
                    if(reused){
                        $scope.$root.showAlert($filter('translate')('SMT26.msg.this_reel_used'));
                    }else{
                        let content = {
                            reel_no:barcode
                        }
                        $MMWService.sendToServer({
                            uri : 'ServicesSMT.Module_SMT.stock_pick_reel_info_get',
                            content,
                            success : function(data){
                                if(data.reel_list.length!=0){
                                    let reel_list = angular.copy(data.reel_list);
                                    $scope.reel_info = reel_list;
                                    $scope.reel_info[0].is_msd = $scope.reel_info[0].is_msd=='1'?$filter('translate')('SMT26.msg.is_msd'):$filter('translate')('SMT26.msg.not_msd')
                                    let match = false;
                                    for(let i =0;i<$scope.detail_list.length;i++){
                                        if($scope.detail_list[i].material_no == $scope.reel_info[0].material_no){
                                            match = true;
                                            $scope.real_need_qty = $scope.detail_list[i].amount_need;
                                            key = i;
                                        }
                                        $scope.detail_list[i].is_msd = data.reel_list[0].is_msd;
                                    }
                                    if(!match){
                                        $scope.reel_info = [];
                                        $scope.$root.showAlert($filter('translate')('SMT26.msg.no_reel_match'));
                                    }
                                }else{
                                    $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                                }
                            }
                        });
                    }
                    
                }
            }

             // QRCode輸入
             $scope.QRCodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        $scope.new_reel_no = result.text;
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('common.msg.cant_use_camera'));
                    },
                    orientation : 'portrait'
                });
            };

            $scope.getNowTime = function(){
                return moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
            }

            $scope.search = function(condition){
                var content = {
                    query_condition:condition
                };
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.stock_pick_info_get',
                    content,
                    success : function(data){
                        $scope.detail_list =[];
                        $scope.pick_list = data.pick_list;
                        $scope.pick_list.forEach(function(item){
                            item.is_selected = false;
                        })
                    },
                    error_callback:function(data){
                        $scope.pick_list = [];
                    }
                });
            };

            $scope.confirm = function(){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.stock_pick_save_process',
                    content:{
                        detail_list:$scope.detail_list
                    },
                    success : function(data){
                        $scope.detail_list = [];
                        $scope.pick_list = [];
                        $rootScope.showToast($filter('translate')('common.msg.success'));
                    }
                });
            }

            $scope.choose = function(){
                reel_used_list.push($scope.reel_info[0].reel_no);
                if((+$scope.detail_list[key].amount_need) > (+$scope.reel_info[0].actual_qty)){//如果料站口需求比實際物料有的數量多
                    $scope.detail_list[key].reel_no = $scope.reel_info[0].reel_no;//將料捲的資料帶入
                    $scope.detail_list[key].amount = $scope.reel_info[0].actual_qty;//將最多數需求帶入
                    let remain_amount_need = (+$scope.detail_list[key].amount_need) - (+$scope.reel_info[0].actual_qty);
                    $scope.real_need_qty = remain_amount_need;
                    $scope.detail_list[key].amount_need = $scope.reel_info[0].actual_qty;
                    if($scope.detail_list[key].status_type != 'ADD'){
                        $scope.detail_list[key].status_type = 'UPD';
                    }
                    let remain_detail_item = angular.copy($scope.detail_list[key]);
                    remain_detail_item.amount_need = remain_amount_need;
                    remain_detail_item.reel_no = '';
                    remain_detail_item.amount = '';
                    remain_detail_item.status_type = 'ADD';
                    $scope.detail_list.splice(key+1,0,remain_detail_item);//將不足的數量新增一個欄位插入detail_list
                    $scope.reel_info = '';
                    $scope.real_need_qty = '';
                    return;
                }else if((+$scope.detail_list[key].amount_need) <= (+$scope.reel_info[0].actual_qty)){//如果料站口需求的material數量小於等於實際物料有的數量
                    $scope.detail_list[key].reel_no = $scope.reel_info[0].reel_no;//將料捲的資料帶入
                    $scope.detail_list[key].amount = $scope.detail_list[key].amount_need;
                    $scope.detail_list[key].status_type = 'UPD';
                    $scope.reel_info = '';
                    $scope.real_need_qty = '';
                    return;
                }
            }

            let last_pick;//單選，儲存上次紀錄
            $scope.getContent = function(item){
                if(last_pick){
                    last_pick.is_selected = false;
                }
                item.is_selected = true;
                last_pick = item;
                let content = {
                    picklist_id:item.picklist_id
                }
                $MMWService.sendToServer({
                    uri: 'ServicesSMT.Module_SMT.stock_pick_detail_info_get',
                    content,
                    success: function (data) {
                        $scope.detail_list = data.detail_list;
                        $scope.detail_list.forEach(function(item_info){
                            item_info.time_stamp = moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
                            item_info.picklist_id = item.picklist_id;
                            item_info.picklist_type = item.picklist_type;
                        })
                    }
                });
            }

            $scope.closeItem = function(item,key,e){
                $ClosItem.init({
                    key,
                    reason_info:item,
                    pick_list:$scope.pick_list,
                    info_list:$scope.data,
                })
                e.stopPropagation();
                e.preventDefault();
            }

            $scope.deleteItem = function(item,key){
                $scope.detail_list.splice(key,1);
            }
        }
    ];
});
